from django.db import models
from django.db.models import F

from main.utils.models import CoreModel


class PlatformInfo(CoreModel):
    name = models.CharField(max_length=255, verbose_name='平台名称')
    desc = models.CharField(max_length=255, verbose_name='描述')
    class Meta:
        db_table = "report_platform_info"

class ModuleInfo(CoreModel):
    name = models.CharField(max_length=255, verbose_name='模块名称')
    desc = models.CharField(max_length=255, verbose_name='描述')
    platform_id = models.IntegerField(verbose_name="平台id")
    class Meta:
        db_table = "report_module_info"

class ReportInfo(CoreModel):
    name = models.CharField(max_length=255, verbose_name='报表名称')
    desc = models.CharField(max_length=255, verbose_name='描述')
    module_id = models.IntegerField(verbose_name="模块id")
    
    class Meta:
        db_table = "report_report_info"

class ReportInterface(CoreModel):
    report_id = models.IntegerField(verbose_name="报表id")
    interface_name= models.CharField(max_length=255, verbose_name='接口名称')
    interface_code= models.CharField(max_length=255, verbose_name='接口编码',unique=True)
    interface_desc = models.TextField(verbose_name='接口描述')
    interface_db_type = models.CharField(max_length=255, verbose_name='数据库类型')
    interface_db_name = models.CharField(max_length=255, verbose_name='数据库名称')
    interface_sql = models.TextField(verbose_name='接口sql')
    is_total = models.CharField(default='0', max_length=1, verbose_name='是否合计',choices=(('1', '是'), ('0', '否')))
    is_paging = models.CharField(default='0', max_length=1, verbose_name='是否分页',choices=(('1', '是'), ('0', '否')))
    is_date_option = models.CharField(default='0', max_length=1,verbose_name='是否日期查询',choices=(('1', '是'), ('0', '否')))
    is_second_table = models.CharField(default='0', max_length=1,verbose_name='二级表头',choices=(('1', '是'), ('0', '否')))
    is_login_visit = models.CharField(default='0', max_length=1,verbose_name='是否登陆验证',choices=(('1', '是'), ('0', '否')))
    alarm_type = models.CharField(default='0', max_length=1,verbose_name='报警类型',choices=(('0','否'),('1', '邮件'), ('2', '短信'), ('3', '钉钉'), ('4', '企业微信'), ('5', '电话')))
    report_name = models.CharField(max_length=255, verbose_name='报表名称', null=True, blank=True)
    module_name = models.CharField(max_length=255, verbose_name='模块名称', null=True, blank=True)
    platform_name = models.CharField(max_length=255, verbose_name='平台名称', null=True, blank=True)
    user_name = models.CharField(max_length=255, verbose_name='用户名称', null=True, blank=True)

    class Meta:
        db_table = "report_interface_info"
    

# 接口数据类型 输出参数(1字符 2整数 3小数 4百分比) 输入参数(11日期 12月份 13单选 14多选 15文本)
class InterfaceFields(CoreModel):
    data_type = (
        ('1','字符'),
        ('2','整数'),
        ('3','小数'),
        ('4','百分比'),
        ('5','无格式整数'),
        ('6','无格式小数'),
        ('7','无格式百分比'),
        ('8','1位百分比'),
        ('9','1位小数'),
        ('10','年份'),
        ('11','日期'),
        ('12','月份'),
        ('13','单选'),
        ('14','多选'),
        ('15','文本')
    )

    para_type =(
        ('1','输入参数'),
        ('2','输出参数'),
    )
    interface_id = models.IntegerField(verbose_name='接口id', null=True, blank=True)
    interface_para_code = models.CharField(max_length=255, verbose_name='接口参数编码')
    interface_para_name = models.CharField(max_length=255, verbose_name='接口参数名称')
    interface_para_position = models.IntegerField(verbose_name='接口参数位置')
    interface_para_type = models.CharField(max_length=255, verbose_name='接口参数类型', choices=para_type)
    interface_data_type = models.CharField(max_length=255, verbose_name='接口参数数据类型', choices=data_type)
    interface_para_default = models.CharField(max_length=255, verbose_name='接口参数默认值', null=True, blank=True)

    interface_para_rowspan = models.IntegerField(verbose_name='接口参数跨行', null=True, blank=True)
    interface_parent_name = models.CharField(max_length=255, verbose_name='接口参数父级名称',null=True, blank=True)
    interface_parent_position = models.IntegerField(verbose_name='接口参数父级位置',null=True, blank=True)
    interface_para_interface_code = models.CharField(max_length=255, verbose_name='接口参数接口编码',null=True, blank=True)
    interface_cascade_para = models.CharField(max_length=255, verbose_name='接口参数级联参数',null=True, blank=True)
    interface_show_flag = models.CharField(max_length=255, verbose_name='接口参数是否显示', choices=(('1', '是'),('0','否')), default='1')
    interface_export_flag= models.CharField(max_length=255, verbose_name='接口参数是否导出',choices=(('1', '是'), ('0', '否')), default='1')
    interface_show_desc = models.CharField(max_length=255, verbose_name='接口参数显示名称',null=True, blank=True)
    interface_para_desc = models.CharField(max_length=255, verbose_name='接口参数描述',null=True, blank=True)

    class Meta:
        db_table = "report_interface_fields"
    
    

class DataSource(CoreModel):
    datasource_name = models.CharField(max_length=255, verbose_name='数据源名称')
    db_type = models.CharField(max_length=255, verbose_name='数据源类型')
    db_name = models.CharField(max_length=255, verbose_name='数据库名称')
    db_user = models.CharField(max_length=255, verbose_name='数据库用户名')
    db_pwd = models.CharField(max_length=255, verbose_name='数据库密码')
    db_port = models.IntegerField(verbose_name='数据库端口')
    db_server = models.CharField(max_length=255, verbose_name='数据库地址')
    
    
    class Meta:
        db_table="report_datasource"
        


        
class DataTable(CoreModel):
    table_name = models.CharField(max_length=255, verbose_name='表名')
    table_desc = models.CharField(max_length=255, verbose_name='描述')
    table_schema = models.CharField(max_length=255, verbose_name='表模式', null=True, default="")
    schema_type = models.CharField(max_length=255, verbose_name='模式类型', default='')
    class Meta:
        db_table = "report_table_info"

class TableColumn(CoreModel):
    # table_id = models.IntegerField(verbose_name='表id')
    table = models.ForeignKey(DataTable, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=255, verbose_name='字段名')
    desc = models.CharField(max_length=255, verbose_name='字段描述')
    column_order = models.IntegerField(verbose_name='字段排序', default=-1)
    data_type = models.CharField(max_length=255, verbose_name='字段数据类型')
    data_length = models.IntegerField(verbose_name='字段长度', null=True)
    is_primary_key = models.CharField(max_length=255, verbose_name='是否主键', choices=(('1', '是'), ('0', '否')), null=True)
    is_nullable = models.CharField(max_length=255, verbose_name='是否为空', choices=(('1', '是'), ('0', '否')), null=True)
    is_unique_key = models.CharField(max_length=255, verbose_name='是否唯一', choices=(('1', '是'), ('0', '否')), null=True)
    
    class Meta:
        db_table = "report_table_column"

class TableMapping(CoreModel):
    table_id = models.IntegerField(verbose_name='表id', null=True)
    table_schema = models.CharField(max_length=255, verbose_name='表模式',null=True)
    schema_type = models.CharField(max_length=255, verbose_name='模式类型', null=True)
    table_name = models.CharField(max_length=255, verbose_name='表名',null=True)
    src_table_id = models.IntegerField(verbose_name='源表id', null=True)
    src_table_schema = models.CharField(max_length=255, verbose_name='源表模式', null=True)
    src_schema_type = models.CharField(max_length=255, verbose_name='源模式类型', null=True)
    src_table_name = models.CharField(max_length=255, verbose_name='源表名', null=True)
    class Meta:
        db_table = "report_table_mapping"


class TableColumnMapping(CoreModel):
    table_id = models.IntegerField(verbose_name='表id')
    
    class Meta:
        db_table = "report_table_column_mapping"
